import { _HttpClient } from "@delon/theme";
import { Injectable } from "@angular/core";
import { ApiService } from '@core/api.service';
import { JzjcMainService } from './jzjc_main.service';
import { LocalStorage } from '@core/local.storage';
import { TreeTools } from '@core/treeTools.tool';

@Injectable({ providedIn: 'root' })

export class JzjcTool {

    // cjList: any[] = [];
    fxtList: any[] = [];
    czList: any[] = [];
    status: any = {};
    gqList: any[] = [];
    wtlxList: any[] = [];
    wtdjList: any = {};
    wtdjArr: any[] = [];
    bmList: any[] = [];
    bmAllList: any[] = [];
    peopleList: any[] = [];
    userInfo = JSON.parse(this.ls.get('loginInfo'));
    fxtInfo: any = {};

    /**
     * 台类型
     * hkgst 沪昆高速台
     * nkkzt 南昆客专台
     * nkhgpst 南昆、红果普速台
     * hkpst 沪昆普速台
     */
    fxtType = "";
    fxtArr: any = {};



    constructor(
        private apiService: ApiService,
        private jzjcMainService: JzjcMainService,
        public ls: LocalStorage,
        private treeTools: TreeTools,
    ) { }

    initCmp() {
        this.initUser();
        this.initStatus();
        this.initFxt();
        // this.getCz(type);

        this.initWtlx();
        this.initWtdj();
        this.getBm();
        this.getPeople();
    }

    initUser() {
        this.userInfo = JSON.parse(this.ls.get('loginInfo'));
    }

    initStatus(tabtype?) {
        this.status.statusList = {
            '0': '未处理',
            '1': '下发车间',
            '2': '已签收',
            '3': '上报调度',
            '4': '申请延期',
            '5': '延期同意',
            '6': '延期驳回',
            '7': '申请销号',
            '8': '销号同意',
            '9': '销号驳回',
            '10': '逾期销号',
            '11': '下发工区',
            '12': '上报车间',
            '13': '上报技术科',
            'cancle_3': '逾期未整改',
        }

        switch (tabtype) {
            case '3': // 未整改 
                this.status.statusArr = [
                    { opValue: '0', opName: '未处理' },
                    { opValue: '1', opName: '下发车间' },
                    { opValue: '2', opName: '已签收' },
                    { opValue: '3', opName: '上报调度' },
                    { opValue: '4', opName: '申请延期' },
                    { opValue: '5', opName: '延期同意' },
                    { opValue: '6', opName: '延期驳回' },
                    { opValue: '7', opName: '申请销号' },
                    { opValue: '9', opName: '销号驳回' },
                    { opValue: '11', opName: '下发工区' },
                    { opValue: '12', opName: '上报车间' },
                    { opValue: '13', opName: '上报技术科' },
                    { opValue: 'cancle_3', opName: '逾期未整改' },

                ];
                break;
            case '4': // 已销号
                this.status.statusArr = [
                    { opValue: '8', opName: '销号同意' },
                    { opValue: '10', opName: '逾期销号' },
                ];
                break;
            case '5': // 待处理
                this.status.statusArr = [
                    { opValue: '0', opName: '未处理' },
                    { opValue: '3', opName: '上报调度' },
                    { opValue: '12', opName: '上报车间' },
                    { opValue: '13', opName: '上报技术科' },
                    { opValue: '4', opName: '申请延期' },
                    { opValue: '7', opName: '申请销号' },
                    { opValue: 'cancle_3', opName: '逾期未整改' },
                ];
                break;
            default:
                this.status.statusArr = [
                    { opValue: '0', opName: '未处理' },
                    { opValue: '1', opName: '下发车间' },
                    { opValue: '2', opName: '已签收' },
                    { opValue: '3', opName: '上报调度' },
                    { opValue: '4', opName: '申请延期' },
                    { opValue: '5', opName: '延期同意' },
                    { opValue: '6', opName: '延期驳回' },
                    { opValue: '7', opName: '申请销号' },
                    { opValue: '8', opName: '销号同意' },
                    { opValue: '9', opName: '销号驳回' },
                    { opValue: '10', opName: '逾期销号' },
                    { opValue: '11', opName: '下发工区' },
                    { opValue: '12', opName: '上报车间' },
                    { opValue: '13', opName: '上报技术科' },
                    { opValue: 'cancle_3', opName: '逾期未整改' },
                ];
                break;
        }

        // 销号状态cancel
        this.status.cancelList = {
            '0': '否',
            '1': '提交',
            '2': '是',
            '3': '逾期未整改',
        }

    }
    initFxt() {
        // this.fxtType = type;
        this.fxtArr = {
            hkgst: '沪昆高速台',
            nkkzt: '南昆客专台',
            nkhgpst: '南昆、红果普速台',
            hkpst: '沪昆普速台',
            all: '全部',
        };

        this.fxtList = [
            { opValue: 'hkgst', opName: '沪昆高速台' },
            { opValue: 'nkkzt', opName: '南昆客专台' },
            { opValue: 'nkhgpst', opName: '南昆、红果普速台' },
            { opValue: 'hkpst', opName: '沪昆普速台' },
        ];
    }
    initWtlx() {
        this.jzjcMainService.getWtlx().subscribe(res => {
            this.wtlxList = res || [];
        })
        // this.wtlxList = [
        //     { opValue: '电源', opName: '电源' },
        //     { opValue: '道岔', opName: '道岔' },
        //     { opValue: '轨道电路', opName: '轨道电路' },
        //     { opValue: '信号机', opName: '信号机' },
        //     { opValue: '系统功能', opName: '系统功能' },
        //     { opValue: '列控设备', opName: '列控设备' },
        //     { opValue: '网络设备', opName: '网络设备' },
        //     { opValue: '其它', opName: '其它' }
        // ];
    }
    initWtdj() {
        this.wtdjArr = [
            { opValue: '1', opName: 'I' },
            { opValue: '2', opName: 'II' },
            { opValue: '3', opName: 'III' }
        ];
        this.wtdjList = {
            '1': 'I',
            '2': 'II',
            '3': 'III',
        }
    }
    getBm() {
        if (this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40') { // 调度
            this.jzjcMainService.getBm_dd().subscribe((res: any) => {
                if (res.data && res.data.length) {
                    this.treeTools.formatTreeNodes({
                        nodes: res.data[0],
                        selectOnlyLeaf: true,
                        keyName: 'ID',
                        valueName: 'NAME',
                    });
                    this.bmList = res.data[0];
                }
            });
        } else {
            if (this.userInfo.N_LEVEL !== 10 || this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
                this.jzjcMainService.getBm().subscribe((res: any) => {
                    if (res.data && res.data.length) {
                        this.bmList = res.data[0];
                    }
                });
            }
        }
        this.jzjcMainService.getBm('1').subscribe((res: any) => {
            if (res.data && res.data.length) {
                this.bmAllList = res.data[0];
            }
        });
    }
    getPeople(id?) {
        // this.jzjcMainService.getPeople(id || this.userInfo.VC_ORG$ID).subscribe(res => {
        //     this.peopleList = res;
        //     this.treeTools.formatTreeNodes({
        //         nodes: this.peopleList,
        //         selectOnlyLeaf: true
        //     });
        // });
        this.apiService.chargesData(id || this.userInfo.VC_ORG$ID).subscribe(res => {
            // res.unshift({ opValue: "", opName: "请选择" })
            this.peopleList = res;
        });
    }
    /**
     * 删除私立字段
     * @param param 
     */
    removePrivateFields(param) {
        Object.keys(param).forEach(key => {
            if (!(key.indexOf('_'))) {
                delete param[key];
            }
        });
    }

    /**
     * 删除后台不接受的字段
     * @param param
     */
    removeUnacceptableFields(param) {
        Object.keys(param).forEach(key => {
            switch (key) {
                case 'createdate':
                    delete param[key];
                    break;
                default:
                    break;
            }
        });
    }
    /**
     * 删除null字段
     * @param param
     */
    removeUselessFields(param) {
        Object.keys(param).forEach(key => {
            if (param[key] == null || typeof (param[key]) === 'undefined' || param[key] === '') {
                delete param[key];
            }
        });
    }
    // getCj() {
    //     this.apiService.getAllChejian('1').subscribe(res => {
    //         this.cjList = res;
    //     });
    // }
    getCz(pc, func?) {
        if (!pc) {
            return;
        }
        this.jzjcMainService.getStationByjcpc(pc).subscribe(res => {
            if (res && res.data && res.data.length) {
                this.czList = res.data[0];
            }
            func && func();
        });
    }

    numToCn(num) {
        const str = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        const numStr = num + "";
        let resStr = "";
        for (const char of numStr) {
            resStr += str[char];
        }
        return resStr;
    }


    replaceBr(handleinfo) {
        if (handleinfo) {
            return handleinfo.replace(/(\r\n|\n|\r)/gm, "<br/>")
        } else {
            return handleinfo;
        }
    }
}